/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'notes_params/' + id,
                model: {
                    url: _global.url.api + 'notes_params',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                        '.edit_listorder:change': 'setlistorder'
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        tpl = _this.getTpl(_this.data.type, _this.data);

                        _this.dialog = dialog({
                            title: '素材修改',
                            content: tpl,
                            width: 600,
                            ok: function () {
                                if (_this.data.type == 1) {
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    _this.data.title = $('#title').val();
                                    _this.data.intro = $('#intro').val();
                                    _this.data.note_id = id;
                                } else if (_this.data.type == 2) {
                                    _this.data.image = $("input[name='imgs']").val();
                                    _this.data.link = $('#link').val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    _this.data.note_id = id;
                                    _this.data.intro = $('#intro').val();
                                } else if (_this.data.type == 3) {
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    _this.data.goods_id = $("input[name='goods_id']").val();
                                    if (_this.data.goods_id == '') {
                                        Alert({msg: '请输入商品id', type: 'error'});
                                        return false;
                                    }
                                    _this.data.intro = $('#intro').val();
                                    _this.data.note_id = id;
                                } else if (_this.data.type == 4) {
                                    var imgs = new Array();
                                    var falg = false;
                                    $("input[name='imgs']").each(function () {
                                        if ($(this).val() != '') {
                                            imgs.push($(this).val());
                                            falg = true;
                                        } else {
                                            falg = false;
                                            return false;
                                        }
                                    });
                                    _this.data.imgs = imgs.join(',');
                                    if (!falg || imgs.length < 2) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                    var goods = new Array();
                                    var goodsfalg = false;
                                    $("input[name='goods_id']").each(function () {
                                        if ($(this).val() != '') {
                                            goods.push($(this).val());
                                            goodsfalg = true;
                                        } else {
                                            goodsfalg = false;
                                            return false;
                                        }
                                    });
                                    _this.data.goods_id = goods.join(',');
                                    if (!goodsfalg) {
                                        Alert({msg: '请填入商品id', type: 'error'});
                                        return false;
                                    }
                                    _this.data.intro = $('#intro').val();
                                    _this.data.note_id = id;
                                } else if (_this.data.type == 5) {
                                    _this.data.title = $('#title').val();
                                    if (_this.data.title == '') {
                                        Alert({msg: '请输入段落标题', type: 'error'});
                                        return false;
                                    }
                                    _this.data.note_id = id;
                                    _this.data.intro = $('#intro').val();
                                }
                                else if (_this.data.type == 6) {
                                    _this.data.content = $("#content").val();
                                    if (_this.data.content == '') {
                                        Alert({msg: '请输入段落正文', type: 'error'});
                                        return false;
                                    }
                                    var parms = new Array();
                                    $("input[name='link_info']").each(function () {
                                        parms.push($(this).val());
                                    });
                                    _this.data.params = parms.join('||');
                                    _this.data.note_id = id;
                                    _this.data.intro = $('#intro').val();
                                } else if (_this.data.type == 7) {
                                    _this.data.video = _this.video;
                                    _this.data.height = $("input[name='height']").val();
                                    if (_this.data.height == '' || _this.data.height == undefined) {
                                        Alert({msg: '请填写图片高度', type: 'error'});
                                        return false;
                                    }
                                    _this.data.width = $("input[name='width']").val();
                                    if (_this.data.width == '' || _this.data.height == undefined) {
                                        Alert({msg: '请填写图片宽度', type: 'error'});
                                        return false;
                                    }
                                    _this.data.image = $("input[name='imgs']").val();
                                    if (_this.data.image == '' || _this.data.image == undefined) {
                                        Alert({msg: '请上传图片', type: 'error'});
                                        return false;
                                    }
                                }

                                if (model.put()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        if (_this.data.type == 6) {
                            $.each(_this.data.info, function (i, v) {
                                if (v.params != undefined) {
                                    $("#links_list").append(template('info-tpl', v.params));
                                }
                            });
                            $("#links_show").show();
                        }
                        $('input,select,textarea').on('change', function (e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }, setlistorder: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var model = this;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if (new Model({
                                url: _global.url.api + 'notes_params_set',
                                data: {id: id, listorder: listorder, act: 'order'}
                            }).put()) {
                            _this.collection.fetch();
                        }
                        ;
                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        getTpl: function (type, data) {
            if (type == 1) {
                return template('topmodel-tpl', data);
            } else if (type == 2) {
                return template('goodsimage-tpl', data);
            } else if (type == 3) {
                return template('goodsone-tpl', data);
            } else if (type == 4) {
                return template('goodstwo-tpl', data);
            } else if (type == 5) {
                return template('title-tpl', data);
            }
            else if (type == 6) {
                return template('content-tpl', data);
            } else if (type == 7) {
                return template('video-tpl', data);
            }
        },
        event: function () {
            var _this = this;
            //保存
            $('#btn-topmodel').on('click', function (e) {
                _this.showDialog(e, 1, _this);
            });
            $('#btn-goodsimage').on('click', function (e) {
                _this.showDialog(e, 2, _this);
            });
            $('#btn-goodsone').on('click', function (e) {
                _this.showDialog(e, 3, _this);
            });
            $('#btn-goodstwo').on('click', function (e) {
                _this.showDialog(e, 4, _this);
            });
            $('#btn-title').on('click', function (e) {
                _this.showDialog(e, 5, _this);
            });
            $("#btn-content").on('click', function (e) {
                _this.showDialog(e, 6, _this);
            });
            $("#btn-video").on('click', function (e) {
                _this.showDialog(e, 7, _this);
            });
            $('body').on('click', '#add_link', function (e) {
                _this.addLink();
            });
            $('body').on('click', '#btn-search', function (e) {
                var keyword = $('#keyword').val();
                var content = $("#content").val();
                if (content == "") {
                    Alert({'msg': '正文不能为空', type: 'error'});
                    return false;
                }
                if (keyword == '') {
                    return false;
                }
                _this.searchPostion(keyword, content);

            });
            $('body').on('click', '.remove_link_edit', function (e) {
                console.log(111);
                e.preventDefault();
                $(this).parents("tr .link_tr_list").remove();
            });

            //上传视频
            $('body').on('click', '#video', function (e) {
                e.preventDefault();
                _this.upload_dialog = dialog({
                    content: template('upload-video-tpl', {}),
                    width: 550,
                    ok: function () {
                        var video = $("#upload-frame1")[0].contentWindow.video.get();
                        _this.videoPreview(video);
                        console.log(video);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $("body").on("click", '.video-remove', function (e) {
                e.preventDefault();
                $(this).parent('li').remove();
            });
            $('body').on('click', '.remove_link_edit', function (e) {
                console.log(111);
                e.preventDefault();
                $(this).parents("tr .link_tr_list").remove();
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('body').on('click', '.btn-upload', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreviews(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        showDialog: function (e, type, _this) {
            _this.data = {};
            e.preventDefault();
            tpl = _this.getTpl(type, {info: [{}, {}]});

            _this.dialog = dialog({
                title: '添加素材',
                content: tpl,
                width: 600,
                ok: function () {
                    if (type == 1) {
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.title = $('#title').val();
                        _this.data.intro = $('#intro').val();
                        _this.data.note_id = id;
                        _this.data.type = type;
                    } else if (type == 2) {
                        _this.data.image = $("input[name='imgs']").val();
                        _this.data.link = $("input[name='link']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.note_id = id;
                        _this.data.intro = $('#intro').val();
                        _this.data.type = type;
                    } else if (type == 3) {
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.goods_id = $("input[name='goods_id']").val();
                        if (_this.data.goods_id == '') {
                            Alert({msg: '请输入商品id', type: 'error'});
                            return false;
                        }
                        _this.data.intro = $('#intro').val();
                        _this.data.note_id = id;
                        _this.data.type = type;
                    } else if (type == 4) {
                        var imgs = new Array();
                        var falg = false;
                        $("input[name='imgs']").each(function () {
                            if ($(this).val() != '') {
                                imgs.push($(this).val());
                                falg = true;
                            } else {
                                falg = false;
                                return false;
                            }
                        });
                        _this.data.imgs = imgs.join(',');
                        if (!falg || imgs.length < 2) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        var goods = new Array();
                        var goodsfalg = false;
                        $("input[name='goods_id']").each(function () {
                            if ($(this).val() != '') {
                                goods.push($(this).val());
                                goodsfalg = true;
                            } else {
                                goodsfalg = false;
                                return false;
                            }
                        });
                        _this.data.goods_id = goods.join(',');
                        if (!goodsfalg) {
                            Alert({msg: '请填入商品id', type: 'error'});
                            return false;
                        }
                        _this.data.intro = $('#intro').val();
                        _this.data.note_id = id;
                        _this.data.type = type;
                    } else if (type == 5) {
                        _this.data.title = $('#title').val();
                        if (_this.data.title == '') {
                            Alert({msg: '请输入段落标题', type: 'error'});
                            return false;
                        }
                        _this.data.note_id = id;
                        _this.data.intro = $('#intro').val();
                        _this.data.type = type;
                    }
                    else if (type == 6) {
                        _this.data.content = $("#content").val();
                        if (_this.data.content == '') {
                            Alert({msg: '请输入段落正文', type: 'error'});
                            return false;
                        }
                        var parms = new Array();
                        $("input[name='link_info']").each(function () {
                            parms.push($(this).val());
                        });
                        _this.data.params = parms.join('||');
                        _this.data.note_id = id;
                        _this.data.intro = $('#intro').val();
                        _this.data.type = type;
                    } else if (type == 7) {
                        //_this.data.video = $('#video').val();
                        _this.data.video = _this.video;
                        _this.data.height = $("input[name='height']").val();
                        if (_this.data.height == '' || _this.data.height == undefined) {
                            Alert({msg: '请填写图片高度', type: 'error'});
                            return false;
                        }
                        _this.data.width = $("input[name='width']").val();
                        if (_this.data.width == '' || _this.data.height == undefined) {
                            Alert({msg: '请填写图片宽度', type: 'error'});
                            return false;
                        }
                        _this.data.image = $("input[name='imgs']").val();
                        if (_this.data.image == '' || _this.data.image == undefined) {
                            Alert({msg: '请上传图片', type: 'error'});
                            return false;
                        }
                        _this.data.note_id = id;
                        _this.data.type = type;
                        //_this.data.video = _this.videoUrl;
                    }
                    if (new Model({
                            url: _global.url.api + 'notes_params',
                            data: _this.data
                        }).post()) {
                        _this.collection.reload();
                    }
                },
                cancel: function () {
                }
            });
            _this.dialog.show();
            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
        },

        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        },
        imgPreviews: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view' + img.item).html(template('img-tpl', img));
            }
        },
        videoPreview: function (video) {
            var _this = this;
            console.log(video.id);
            if (typeof video.id !== 'undefined') {
                $('#video-view').html(template('video-tpls', video));
            }
            this.video = video;
        },

        addLink: function (e) {
            var html = template('link_list-tpl');
            dialog({
                'content': html,
                ok: function () {
                    var postions = new Array();
                    $("input[name='links']:checked").each(function () {
                        postions.push({start: $(this).val(), length: $(this).attr('attr'), url: $("#url").val()});
                    });
                    var check_pos = postions.join(',');
                    if (check_pos == '') {
                        Alert({'msg': '请选择要添加链接位置', type: 'error'});
                        return false;
                    }
                    if ($("#url").val() == '') {
                        Alert({'msg': '请输入url链接', type: 'error'});
                        return false;
                    }
                    $.each(postions, function (i, v) {
                        console.log(v)
                        $("#links_list").append(template('info-tpl', v))
                    });
                    $("#links_show").show();
                }, cancel: function () {
                }
            }).show();
        },
        searchPostion: function (keyword, str) {
            var positions = [];
            var pos = str.indexOf(keyword);

            while (pos > -1) {
                positions.push({start: pos, length: keyword.length});
                pos = str.indexOf(keyword, pos + 1);
            }
            $.each(positions, function (i, v) {
                $("#link_data-list").append(template('link-tpl', v))
            });
        }
    };

    new index();

});